<template>
  <a-modal
    v-model:open="props.computeExportVisible"
    :title="title"
    class="addStation"
    destroyOnClose
    :width="520"
    @cancel="cancel"
    centered
  >
    <a-range-picker
      show-time
      format="YYYY-MM-DD HH:mm:ss"
      @change="onRangeChange"
    ></a-range-picker>
    <template #footer>
      <a-button key="back" @click="cancel">取消</a-button>
      <a-button
        key="submit"
        type="primary"
        :loading="loading"
        @click="confirmDate"
        >确认</a-button
      >
    </template>
  </a-modal>
</template>
<script setup>
// 
import utilsExportFile from "@/utils/exportFile"

const $api = inject("$api")
const props = defineProps({
  title: String,
  isExport: Boolean,
  computeExportVisible: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
  },
})
const submitData = ref({
  pdId: null,
  paramName: null,
  companyName: null,
  start: null,
  end: null,
})

const loading = ref(false)
const emits = defineEmits(["compute:computeExportVisible"])

watch(
  () => props.computeExportVisible,
  (val) => {
    submitData.value.pdId = props.rowData.id
    submitData.value.paramName = props.rowData.paramName
    submitData.value.companyName = props.rowData.companyName
  }
)
const onRangeChange = (value, dateStr) => {
  submitData.value.start = dateStr[0]
  submitData.value.end = dateStr[1]
}

const cancel = () => {
  emits("compute:computeExportVisible", false)
}
const confirmDate = async () => {
  const params = {
    pdId: submitData.value.pdId,
    start: submitData.value.start,
    end: submitData.value.end,
  }
  if (props.isExport) {
    const res = await $api.exportParamDetail(params)
    utilsExportFile.exportFile(
      res,
      submitData.value.companyName + "-" + submitData.value.paramName
    )
  } else {
    const res = await $api.calcPdData(params)
    if (res && res.code === 0) {
      message.success("操作成功")
    }
  }
  cancel()
}
</script>
<style lang="less"></style>
